package net.maku.user.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.mybatis.service.impl.BaseServiceImpl;
import net.maku.user.convert.UserInfoConvert;
import net.maku.user.entity.UserInfoEntity;
import net.maku.user.query.UserInfoQuery;
import net.maku.user.vo.UserInfoVO;
import net.maku.user.dao.UserInfoDao;
import net.maku.user.service.UserInfoService;
import com.fhs.trans.service.impl.TransService;
import net.maku.framework.common.utils.ExcelUtils;
import net.maku.user.vo.UserInfoExcelVO;
import net.maku.framework.common.excel.ExcelFinishCallBack;
import org.springframework.web.multipart.MultipartFile;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 维修信息
 *
 * @author df
 *
 */
@Service
@AllArgsConstructor
public class UserInfoServiceImpl extends BaseServiceImpl<UserInfoDao, UserInfoEntity> implements UserInfoService {
    private final TransService transService;

    @Override
    public PageResult<UserInfoVO> page(UserInfoQuery query) {
        IPage<UserInfoEntity> page = baseMapper.selectPage(getPage(query), getWrapper(query));

        return new PageResult<>(UserInfoConvert.INSTANCE.convertList(page.getRecords()), page.getTotal());
    }


    private LambdaQueryWrapper<UserInfoEntity> getWrapper(UserInfoQuery query){
        LambdaQueryWrapper<UserInfoEntity> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(ObjectUtil.isNotEmpty(query.getName()), UserInfoEntity::getName, query.getName());

        wrapper.eq(ObjectUtil.isNotEmpty(query.getPhone()), UserInfoEntity::getPhone, query.getPhone());

        wrapper.eq(ObjectUtil.isNotEmpty(query.getProvince()), UserInfoEntity::getProvince, query.getProvince());

        wrapper.eq(ObjectUtil.isNotEmpty(query.getCity()), UserInfoEntity::getCity, query.getCity());

        wrapper.eq(ObjectUtil.isNotEmpty(query.getDistrict()), UserInfoEntity::getDistrict, query.getDistrict());

        wrapper.like(ObjectUtil.isNotEmpty(query.getAddress()), UserInfoEntity::getAddress, query.getAddress());

        return wrapper;
    }


    @Override
    public UserInfoVO get(Long id) {
        UserInfoEntity entity = baseMapper.selectById(id);
        UserInfoVO vo = UserInfoConvert.INSTANCE.convert(entity);

        return vo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(UserInfoVO vo) {
        UserInfoEntity entity = UserInfoConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(UserInfoVO vo) {
        UserInfoEntity entity = UserInfoConvert.INSTANCE.convert(vo);

        updateById(entity);


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);

    }



    @Override
    public void export() {
    List<UserInfoExcelVO> excelList = UserInfoConvert.INSTANCE.convertExcelList(list());
        transService.transBatch(excelList);
        ExcelUtils.excelExport(UserInfoExcelVO.class, "维修信息", null, excelList);
    }

}